package com.tianlang.beetl.tag;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.beetl.core.Tag;

import com.jfinal.plugin.activerecord.Record;

/**
 * 自定义权限标签
 * 
 * @author mcp
 *
 */
public class AuthorityTag extends Tag {
	@SuppressWarnings("unchecked")
	@Override
	public void render() {
		Map<String, String> keyValues = (Map<String, String>) args[1];
		String value = keyValues.get("value").toString();
		Subject currentUser = this.getSubject();
		// 如果还未认证
		if (!currentUser.isAuthenticated()) {
			return;
		}
		Record user = (Record) currentUser.getPrincipal();
		if (user == null) {
			return;
		}
		if (user.getStr("username").equals("superadmin")) {
			// 超级管理员具有所有权限
			doBodyRender();
			return;
		}
		// -----------------进行鉴权-------------
		if (currentUser.isPermitted(value)) {
			// 鉴权
			doBodyRender();
			return;
		}
	}

	private Subject getSubject() {
		return SecurityUtils.getSubject();
	}

}
